package com.xingzhi.shortvideosharingplatform.controller;

import com.xingzhi.shortvideosharingplatform.common.Result;
import com.xingzhi.shortvideosharingplatform.dto.VideoLikeDTO;
import com.xingzhi.shortvideosharingplatform.service.VideoLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/video/like")
public class VideoLikeController {

    @Autowired
    private VideoLikeService videoLikeService;

    /**
     * 点赞视频
     */
    @PostMapping
    public Result<Boolean> likeVideo(@RequestBody VideoLikeDTO likeDTO) {
        try {
            // TODO: 从Token中获取用户ID，此处暂时使用传入的userId
            Long userId = likeDTO.getUserId();
            Long videoId = likeDTO.getVideoId();
            
            boolean success = videoLikeService.likeVideo(userId, videoId);
            if (success) {
                return Result.success(true, "点赞成功");
            } else {
                return Result.error(500, "点赞失败，请稍后再试");
            }
        } catch (Exception e) {
            log.error("点赞视频失败", e);
            return Result.error(500, "点赞失败: " + e.getMessage());
        }
    }
    
    /**
     * 取消点赞
     */
    @DeleteMapping
    public Result<Boolean> unlikeVideo(@RequestParam Long userId, @RequestParam Long videoId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean success = videoLikeService.unlikeVideo(userId, videoId);
            if (success) {
                return Result.success(true, "取消点赞成功");
            } else {
                return Result.error(400, "取消点赞失败，可能未点赞过该视频");
            }
        } catch (Exception e) {
            log.error("取消点赞失败", e);
            return Result.error(500, "取消点赞失败: " + e.getMessage());
        }
    }
    
    /**
     * 检查用户是否点赞过视频（√）
     */
    @GetMapping("/check")
    public Result<Boolean> checkLikeStatus(@RequestParam Long userId, @RequestParam Long videoId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean liked = videoLikeService.isVideoLiked(userId, videoId);
            return Result.success(liked);
        } catch (Exception e) {
            log.error("检查点赞状态失败", e);
            return Result.error(500, "检查点赞状态失败: " + e.getMessage());
        }
    }
} 